package com.hmall.search.Listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.hmall.api.client.ItemClient;
import com.hmall.api.dto.ItemDTO;
import com.hmall.common.constants.MQConstants;
import com.hmall.search.domain.po.ItemDoc;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

@Component
@RequiredArgsConstructor
public class ItemListener {


    private final ItemClient itemClient;

    private final RestHighLevelClient client=new RestHighLevelClient(RestClient.builder(
            HttpHost.create("http://192.168.176.129:9200")
    ));
        @RabbitListener(bindings = @QueueBinding(
                value=@Queue(name = MQConstants.ITEM_SAVE_QUEUE ,durable = "true"),
                exchange = @Exchange(name = MQConstants.ITEM_EXCHANGE),
                key = MQConstants.ITEM_SAVE_KEY
        ))
        public void save(Long id) throws IOException {
            ItemDTO itemDTO = itemClient.queryItemById(id);
            ItemDoc itemDoc = BeanUtil.copyProperties(itemDTO
                    , ItemDoc.class);
            //更新实体时间
            itemDoc.setUpdateTime(LocalDateTime.now());
            //json化
            String jsonStr = JSONUtil.toJsonStr(itemDoc);
            //准备参数
            IndexRequest request = new IndexRequest("items").id(itemDoc.getId());
            request.source(jsonStr, XContentType.JSON);
            //发送请求
            client.index(request, RequestOptions.DEFAULT);
            System.out.println("RabbitMQ发送消息，新增成功");
        }


    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name = MQConstants.ITEM_UPDATE_QUEUE ,durable = "true"),
            exchange = @Exchange(name = MQConstants.ITEM_EXCHANGE),
            key = MQConstants.ITEM_UPDATE_KEY
    ))
    public void update(Long id) throws IOException {
        ItemDTO itemDTO = itemClient.queryItemById(id);
        ItemDoc itemDoc = BeanUtil.copyProperties(itemDTO
                , ItemDoc.class);
        //更新实体时间
        itemDoc.setUpdateTime(LocalDateTime.now());
        //json化
        String jsonStr = JSONUtil.toJsonStr(itemDoc);
        //准备参数
        UpdateRequest request = new UpdateRequest("items",itemDoc.getId());
        request.doc(jsonStr, XContentType.JSON);
        //发送请求
        client.update(request, RequestOptions.DEFAULT);
        System.out.println("RabbitMQ发送消息，更新成功");
    }

    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(name = MQConstants.ITEM_DELETE_QUEUE ,durable = "true"),
            exchange = @Exchange(name = MQConstants.ITEM_EXCHANGE),
            key = MQConstants.ITEM_DELETE_KEY
    ))
    public void delete(Long id) throws IOException {

        //准备参数
        DeleteRequest request = new DeleteRequest("items", id.toString());

        //发送请求
        client.delete(request, RequestOptions.DEFAULT);
        System.out.println("RabbitMQ发送消息，删除成功");
    }



}
